package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.common.json.JSON;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.OtherConfig;
import com.kch.system.domain.SysCloumn;
import com.kch.system.service.IOtherConfigService;
import com.kch.system.service.ISysCloumnService;
import com.kch.system.vo.ScienceVO;
import com.kch.web.config.CloumnProperties;
import com.kch.web.controller.common.CheckNumberUtils;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/system/science")
@Api(tags = "科技管家文案")
public class ScienceController extends BaseController {

    private String prefix = "system/science";

    @Autowired
    private IOtherConfigService otherConfigService;
    @Autowired
    private ISysCloumnService sysCloumnService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:science:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        List<String> list = new ArrayList<>();
        list.add(cloumnProperties.getTeque());
        list.add(cloumnProperties.getTedem());
        list.add(cloumnProperties.getTeexp());
        list.add(cloumnProperties.getTepop());
        list.add(cloumnProperties.getTepro());
        List<SysCloumn> sysCloumns = sysCloumnService.selectByCodeList(list);
        mmap.put("clos", sysCloumns);
        return prefix + "/science";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:science:list")
    @ResponseBody
    public TableDataInfo list(OtherConfig config) throws Exception {
        List<String> list = new ArrayList<>();
        list.add(cloumnProperties.getTeque());
        list.add(cloumnProperties.getTedem());
        list.add(cloumnProperties.getTeexp());
        list.add(cloumnProperties.getTepop());
        list.add(cloumnProperties.getTepro());
        startPage();
        List<OtherConfig> configList = otherConfigService.selectByCloCodeList(list, config);
        StringBuilder builder = new StringBuilder();
        for (OtherConfig otherConfig : configList) {
            ScienceVO unmarshal = JSON.unmarshal(otherConfig.getContent(), ScienceVO.class);
            if(cloumnProperties.getTepop().equals(otherConfig.getCloumnCode())){
                builder.append(unmarshal.getParam1());
            }else{
                builder.append(unmarshal.getParam1()).append(",").append(unmarshal.getParam2()).append(",").append(unmarshal.getParam3());
            }
            otherConfig.setContent(builder.toString());
            builder.setLength(0);
        }
        return getDataTable(configList);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<String> list = new ArrayList<>();
        list.add(cloumnProperties.getTeque());
        list.add(cloumnProperties.getTedem());
        list.add(cloumnProperties.getTeexp());
        list.add(cloumnProperties.getTepop());
        list.add(cloumnProperties.getTepro());
        List<SysCloumn> sysCloumns = sysCloumnService.selectByCodeList(list);
        mmap.put("clos", sysCloumns);
        return prefix + "/add";
    }

    @Log(title = "科技管家", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated ScienceVO scienceVO) throws Exception {
        if(!ObjectUtils.isEmpty(scienceVO.getWeight())){
            if(!CheckNumberUtils.check(String.valueOf(scienceVO.getWeight()))){
                return AjaxResult.error("权重数据必须为整型");
            }
        }
        OtherConfig config = new OtherConfig();
        config.setCreateTime(new Date());
        config.setCreateBy(ShiroUtils.getLoginName());
        config.setCloumnCode(scienceVO.getCloumnCode());
        config.setTitle(scienceVO.getTitle());
        config.setWeight(scienceVO.getWeight());
        config.setContent(JSON.marshal(scienceVO));
        otherConfigService.insertOtherConfig(config);
        return toAjax(true);
    }

    @Log(title = "会议类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(otherConfigService.deleteConfig(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        OtherConfig config = otherConfigService.selectById(id);
        ScienceVO scienceVO = JSON.unmarshal(config.getContent(), ScienceVO.class);
        scienceVO.setTitle(config.getTitle());
        scienceVO.setId(config.getId());
        scienceVO.setCloumnCode(config.getCloumnCode());
        scienceVO.setWeight(config.getWeight());
        SysCloumn sysCloumn = sysCloumnService.selectByCode(config.getCloumnCode());
        scienceVO.setCloumnName(sysCloumn.getName());
        mmap.put("science", scienceVO);
        return prefix + "/edit";
    }

    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ScienceVO scienceVO) {
        try {
            if(!ObjectUtils.isEmpty(scienceVO.getWeight())){
                if(!CheckNumberUtils.check(String.valueOf(scienceVO.getWeight()))){
                    return AjaxResult.error("权重数据必须为整型");
                }
            }
            OtherConfig config = new OtherConfig();
            config.setUpdateTime(new Date());
            config.setUpdateBy(ShiroUtils.getLoginName());
            config.setCloumnCode(scienceVO.getCloumnCode());
            config.setTitle(scienceVO.getTitle());
            config.setWeight(scienceVO.getWeight());
            config.setContent(JSON.marshal(scienceVO));
            config.setId(scienceVO.getId());
            otherConfigService.updateById(config);
            return toAjax(true);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

}
